<template>
  <div class="app-container">
    <div v-show="!modifyInfo.visible">
      <!-- 条件栏 -->
      <div class="filter-wrapper">
        <NewPageFilter
          v-model:query="filterInfo.query"
          :filter-list="filterInfo.list"
          :filter-btn="filterInfo.btn"
        >
          <template v-slot:filter-operateTime>
            <el-date-picker
              v-model="operateTime"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              type="datetimerange"
              range-separator="至"
              align="right"
              start-placeholder="生产日期"
              end-placeholder="失效日期"
              :clearable="true"
              @change="timeChange"
            ></el-date-picker>
          </template>
        </NewPageFilter>
      </div>
      <div class="filter-wrapper">
        <NewPageFilter :filter-btn="filterInfo.btnList">
          <template v-slot:filter-open>
            <el-button :disabled="!canOpen" @click="openProduct()"
              >启用</el-button
            >
          </template>
          <template v-slot:filter-stop>
            <el-button :disabled="!canStop" @click="stopProduct()"
              >停用</el-button
            >
          </template>
          <template v-slot:filter-del>
            <el-button :disabled="!canDelete" @click="del()">删除</el-button>
          </template>
        </NewPageFilter>
      </div>
      <!-- 表格 -->
      <TableContainer
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="tableInfo.api"
        :query="filterInfo.query"
        :field-list="tableInfo.fieldList"
        :handle="tableInfo.handle"
        @handleEvent="handleEvent"
      >
        <template v-slot:col-price="scope">
          {{ scope.row.price === -2 ? '-' : scope.row.price }}
        </template>
      </TableContainer>
    </div>
    <Modify
      v-if="modifyInfo.visible"
      v-model:visible="modifyInfo.visible"
      :title="modifyInfo.title"
      :rowData="modifyInfo.rowData"
      @returnModify="getList"
    />
  </div>
</template>

<script setup name="depotProductForbiddenConfig">
const Modify = defineAsyncComponent(() => import('./modify'))
import {
  getDepotProductForbiddenConfigPage,
  delDepotProductForbiddenConfig,
  openDepotProductForbiddenConfig,
  stopDepotProductForbiddenConfig,
} from '@/api/use'
import { getDepotAll, getSupplierAll } from '@/api/core'
const { proxy } = getCurrentInstance()

const operateTime = ref([])
// 过滤相关配置
const filterInfo = ref({
  query: {
    productionDate: operateTime.value[0],
    validityDate: operateTime.value[1],
    sort: 'create_time desc',
    searchType: 'product',
    searchField: 'productId',
  },
  copyQuery: {},
  list: [
    {
      label: '仓库',
      value: 'depotIdMult',
      query: {
        status: 1,
      },
      api: getDepotAll,
      componentType: 'slip-select',
      multiple: true,
      event: search,
    },
    {
      label: '产品名称/别名/编码/助记符',
      value: 'keyword',
      componentType: 'input',
      event: search,
    },
    // {
    //   label: '产品编码',
    //   value: 'productCode',
    //   componentType: 'input',
    //   event: search,
    // },
    {
      label: '供应商',
      value: 'supplierIdMult',
      componentType: 'slip-select',
      api: getSupplierAll,
      query: { sort: 'create_time desc' },
      clearable: true,
      keyField: 'supplierName',
      valueField: 'supplierId',
      multiple: true,
      event: search,
    },
    {
      label: '生产批号',
      value: 'batchNumber',
      componentType: 'input',
      event: search,
    },
    {
      label: '操作时间',
      value: 'operateTime',
      slot: true,
    },
    {
      label: '是否寄售',
      value: 'isConsignBusinessValue',
      asValue: 'isConsignBusiness',
      componentType: 'select-model',
      code: 'yes_or_no',
      event: search,
    },
    {
      label: '是否暂估',
      value: 'isEstimationStockBusinessValue',
      asValue: 'isEstimationStockBusiness',
      componentType: 'select-model',
      code: 'yes_or_no',
      event: search,
    },
    {
      label: '入库批次号',
      value: 'storeBatchNumber',
      componentType: 'input',
      event: search,
    },
    {
      label: '状态',
      value: 'statusValue',
      asValue: 'status',
      componentType: 'select-model',
      code: 'enabled_type',
      event: search,
    },
  ],
  btn: [
    { type: 'button', label: '搜索', event: search },
    { type: 'button', label: '重置', event: clearSearch },
  ],
  btnList: [
    {
      type: 'button',
      label: '新增',
      event: create,
      show: proxy.$permission('depotProductForbiddenConfig:add'),
    },
    {
      type: 'slot',
      value: 'open',
      show: proxy.$permission('depotProductForbiddenConfig:open'),
    },
    {
      type: 'slot',
      value: 'stop',
      show: proxy.$permission('depotProductForbiddenConfig:stop'),
    },
    {
      type: 'slot',
      value: 'del',
      show: proxy.$permission('depotProductForbiddenConfig:del'),
    },
  ],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  api: getDepotProductForbiddenConfigPage,
  data: [],
  selections: [],
  fieldList: [
    { label: '仓库', value: 'depotName' },
    { label: '产品', value: 'productName' },
    { label: '产品编码', value: 'productCode' },
    { label: '供应商', value: 'supplierName' },
    { label: '生产批号', value: 'batchNumber' },
    { label: '生产日期', value: 'productionDate' },
    { label: '失效日期', value: 'validityDate' },
    { label: '单价', value: 'price', type: 'slot' },
    { label: '是否寄售业务', value: 'isConsignBusinessValue' },
    { label: '是否暂估', value: 'isEstimationStockBusinessValue' },
    { label: '入库批次号', value: 'storeBatchNumber' },
    { label: '状态', value: 'statusValue' },
  ],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '120',
    btList: [
      {
        label: '启用',
        event: openProduct,
        show: proxy.$permission('depotProductForbiddenConfig:open'),
        ifRender(row) {
          return row.status === 0
        },
      },
      {
        label: '停用',
        event: stopProduct,
        show: proxy.$permission('depotProductForbiddenConfig:stop'),
        ifRender(row) {
          return row.status === 1
        },
      },
      {
        label: '编辑',
        event: updateProduct,
        show: proxy.$permission('depotProductForbiddenConfig:update'),
        ifRender(row) {
          return row.status === 0
        },
      },
      {
        label: '删除',
        event: del,
        popconfirmText: '删除后将改变数据,是否继续？',
        show: proxy.$permission('depotProductForbiddenConfig:del'),
        ifRender(row) {
          return row.status === 0
        },
      },
    ],
  },
})
const modifyInfo = ref({
  visible: false,
  title: '',
  rowData: {},
})
const canOpen = computed(() => {
  return (
    tableInfo.value.selections.length > 0 &&
    !tableInfo.value.selections.filter((item) => item.status !== 0).length
  )
})
const canStop = computed(() => {
  return (
    tableInfo.value.selections.length > 0 &&
    !tableInfo.value.selections.filter((item) => item.status !== 1).length
  )
})
const canDelete = computed(() => {
  return (
    tableInfo.value.selections.length > 0 &&
    !tableInfo.value.selections.some((item) => item.status === 1)
  )
})

onMounted(() => {
  filterInfo.value.copyQuery = proxy.$fn.deepClone(filterInfo.value.query)
})

function timeChange(val) {
  if (val && val.length) {
    filterInfo.value.query.productionDate = val[0]
    filterInfo.value.query.validityDate = val[1]
  } else {
    filterInfo.value.query.productionDate = ''
    filterInfo.value.query.validityDate = ''
  }
  search()
}
function handleClose() {
  dialogInfo.value.visible = false
}
// 获取列表
function getList() {
  tableInfo.value.refresh = Math.random()
}
function clearSearch() {
  filterInfo.value.query = proxy.$fn.deepClone(filterInfo.value.copyQuery)
  operateTime.value = []
  search()
}
// 搜索
function search() {
  // 重置分页
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
// 触发事件
function handleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      tableInfo.value.selections = [...data]
      break
  }
}
//创建
function create() {
  modifyInfo.value.title = '新增仓库禁领产品'
  modifyInfo.value.rowData = {}
  modifyInfo.value.visible = true
}
function openProduct(row = undefined) {
  const ids = _getIds(row)
  openDepotProductForbiddenConfig(ids).then((resp) => {
    if (resp) {
      getList()
    }
  })
}
function stopProduct(row = undefined) {
  const ids = _getIds(row)
  stopDepotProductForbiddenConfig(ids).then((resp) => {
    if (resp) {
      getList()
    }
  })
}
function updateProduct(row = undefined) {
  modifyInfo.value.title = '编辑仓库禁领产品'
  modifyInfo.value.rowData = row
  modifyInfo.value.visible = true
}
//删除
function del(row = undefined) {
  const ids = _getIds(row)
  delDepotProductForbiddenConfig(ids).then((resp) => {
    if (resp && resp.success) {
      getList()
    }
  })
}
function _getIds(row) {
  let ids = []
  if (row && row.id) {
    ids = [row.id]
  } else {
    if (tableInfo.value.selections.length > 0) {
      ids = tableInfo.value.selections.map((row) => row.id)
    }
  }
  return ids
}
</script>

<style scoped></style>
